$(function() {

	var origin = []
	var map = new AMap.Map('map', {
		resizeEnable: true,
		zoom: 11,
		center: [118.57, 24.82]
	});

	//构造路线导航类
	var driving = new AMap.Driving({
		map: map,
		//		panel: "panel"
	});
	$.ajax({
		type: "POST",
		url: "http://121.199.45.196/Api/CarModel/lists",
		data: "phone=18700781005&pwd=sznanshan",
		success: function(res) {
			console.log(res)
		},
		error: function(res) {
			console.log(res)
		}
	});

	AMapUI.loadUI(['overlay/SimpleMarker'], function(SimpleMarker) {

		var iconStyles = SimpleMarker.getBuiltInIconStyles('fresh');
		origin = [{
				orderid: 1,
				path: [
					[118.5780, 24.8350],
					[118.6260, 24.8311]
				],
				drivername: "黄师傅",
				begin: '晋江',
				end: '晋江'
			},
			{
				orderid: 1,
				path: [
					[118.42, 24.82],
					[118.68, 24.85]
				],
				drivername: "蔡师傅",
				begin: '晋江',
				end: '晋江'
			},
			{
				orderid: 1,

				path: [
					[118.548635, 24.818144],
					[118.638348, 24.883766]
				],
				drivername: "蔡师傅",
				begin: '晋江',
				end: '晋江'
			},
			{
				orderid: 1,
				path: [
					[118.6799, 24.7286],
					[118.7000, 24.7292]
				],
				drivername: "蔡师傅",
				begin: '晋江',
				end: '晋江'
			},
			{
				orderid: 1,
				path: [
					[118.613599, 24.714986],
					[118.692123, 24.756079]
				],
				drivername: "蔡师傅",
				begin: '晋江',
				end: '晋江'
			},
		];
		AMapUI.loadUI(['overlay/SimpleInfoWindow'], function(SimpleInfoWindow) {
			//				var lngLats = getGridLngLats(map.getCenter(), 5, 5);
			for(var i = 0; i < origin.length; i++) {
				$('.orderlist ul').append(`
				<li style="height: 60px;padding: 10px 0;border-top: 1px solid #ccc;cursor: pointer;">
					<div class="am-u-sm-2" style="padding:5px;">
						<div style="border:1px solid #5FB878;margin:0;color:#5FB878;border-radius:50%;width:20px;height:20px;font-size:12px;line-height:20px;text-align:center">${i+1}</div>
					</div>
					<div class = "am-u-sm-6">
						<div class = "am-u-sm-12" style="font-size:12px">开始订单<span>11:26<span></div>
						<div class = "am-u-sm-12" style="font-size:12px">结束订单<span>11:26<span></div>
					</div>
					<div class="am-u-sm-4">
						<div class = "am-u-sm-12" style="font-size:12px;text-align:center">订单完成</div>
						<div class = "am-u-sm-12" style="font-size:12px"><img style="width:100%;height:12px" src="{$static_dir}/assets/img/wuxin.png"></div>
					</div>
				</li>`);
				origin[i].point = new SimpleMarker({
					//普通文本
					iconStyle: iconStyles[2],

					//图标文字
					iconLabel: {
						//A,B,C.....
						innerHTML: i + 1,
						style: {
							//颜色, #333, red等等，这里仅作示例，取iconStyle中首尾相对的颜色
							color: '#fff'
						}
					},
					map: map,
					position: origin[i].path[0],
					path: origin[i].path,
					cont: {
						drivername: origin[i].drivername,
						begin: origin[i].begin,
						end: origin[i].end
					}
				});
			};
			$('.orderlist ul li').on('click', function() {

				getpath(origin[$(this).index()].point, map, driving, SimpleMarker, SimpleInfoWindow)
			});
			//点击修改路线左边
			for(var j = 0; j < origin.length; j++) {
				console.log()
				origin[j].point.on('click', function() {
					getpath(this, map, driving, SimpleMarker, SimpleInfoWindow)
				});
			};
		});
	});
});

function openInfoWin(map, obj, infoWindow) {
	infoWindow.open(map, obj.getPosition());
};

function getpath(obj, map, driving, SimpleMarker, SimpleInfoWindow) {
	var arr;
	var content;

	for(var index in obj) {
		for(var index1 in obj[index]) {
			if(index1 == "cont") {
				idx = index
				arr = obj[index].path
				content = obj[index].cont
			};

		};
	};
	var iconStyles = SimpleMarker.getBuiltInIconStyles('fresh');

	var infoWindow = new SimpleInfoWindow({

		infoTitle: '<strong>' + content.drivername + '</strong>',
		infoBody: '<div>从' + content.begin + '到' + content.end + '</div>',

		//基点指向marker的头部位置
		offset: new AMap.Pixel(0, -31)
	});
	//设置数据

	driving.search(new AMap.LngLat(arr[0][0], arr[0][1]), new AMap.LngLat(arr[arr.length - 1][0], arr[arr.length - 1][1]));

	openInfoWin(map, obj, infoWindow);
	map.setZoomAndCenter(13, arr[arr.length - 1]);
	
	$.ajax({
		type:"get",
		url:"https://admin.jjcxfw.com/ApiCarRegularBus/lists",
		data:"",
		success:function(res){
			
			console.log(res)
			console.log(1)
		}
	});
};
